跳到主要内容

函数

// 写法一
function hello(txt: string): void {
console.log("hello " + txt);
}

// 写法二
const hello: (txt: string) => void = function (txt) {
console.log("hello " + txt);
};

// 写法二:type 优化
type MyFunc = (txt: string) => void;

const hello: MyFunc = function (txt) {
console.log("hello " + txt);
};

// 写法四:interface 优化
interface MyFunc {
(txt: string): void;
}

const hello: MyFunc = (txt) => {
console.log("hello " + txt);
};

参考链接

https://www.typescriptlang.org/docs/handbook/2/functions.html